{
 "cells": [
  {
   "cell_type": "code",
   "id": "initial_id",
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "end_time": "2025-09-03T04:23:45.207628Z",
     "start_time": "2025-09-03T04:23:40.260092Z"
    }
   },
   "source": [
    "# 要训练数据，需要将特征处理为归一化张量，将标签处理为独热编码的张量\n",
    "# transform 模块就用于修改特征和标签张量\n",
    "import torch\n",
    "from torch.utils.tensorboard import SummaryWriter\n",
    "from torchvision import datasets\n",
    "from torchvision.transforms import ToTensor, Lambda\n"
   ],
   "outputs": [],
   "execution_count": 2
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-03T04:23:46.820136Z",
     "start_time": "2025-09-03T04:23:46.782754Z"
    }
   },
   "cell_type": "code",
   "source": [
    "ds = datasets.FashionMNIST(\n",
    "    root='hymenoptera',\n",
    "    train=True,\n",
    "    download=True,\n",
    "    transform=ToTensor(),\n",
    "    target_transform=Lambda(lambda y: torch.zeros(10, dtype=torch.float).scatter_(0, torch.tensor(y), value=1))\n",
    ")"
   ],
   "id": "f2dd8152f8cd2813",
   "outputs": [],
   "execution_count": 3
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-03T04:23:48.446018Z",
     "start_time": "2025-09-03T04:23:48.440794Z"
    }
   },
   "cell_type": "code",
   "source": "len(ds)",
   "id": "699e3665681a90f",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "60000"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 4
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-03T04:23:49.494062Z",
     "start_time": "2025-09-03T04:23:49.476686Z"
    }
   },
   "cell_type": "code",
   "source": "ds[0][0].size()",
   "id": "23ccb7bcc6a7471",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "torch.Size([1, 28, 28])"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 5
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-03T04:25:11.394730Z",
     "start_time": "2025-09-03T04:25:11.374532Z"
    }
   },
   "cell_type": "code",
   "source": "ds[0][1]",
   "id": "84c3c336df8f1a5a",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([0., 0., 0., 0., 0., 0., 0., 0., 0., 1.])"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 6
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-03T04:25:13.642126Z",
     "start_time": "2025-09-03T04:25:13.426485Z"
    }
   },
   "cell_type": "code",
   "source": [
    "writer = SummaryWriter(\"fashion_mnist\")\n",
    "for i in range(10):\n",
    "    img, target = ds[i]\n",
    "    writer.add_image(\"image\", img, i)\n",
    "\n",
    "writer.close()"
   ],
   "id": "8d2a226923d94806",
   "outputs": [],
   "execution_count": 7
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "original_ds = datasets.FashionMNIST(\n",
    "    root='hymenoptera',\n",
    "    train=True,\n",
    "    download=True,\n",
    ")"
   ],
   "id": "99addc33de6fd98f",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "ori_img, ori_label = original_ds[0]\n",
    "type(ori_img)"
   ],
   "id": "139fde66e55c5723",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "# 蜜蜂和蚂蚁数据集\n",
    "from torch.utils.data import Dataset, DataLoader\n",
    "from PIL import Image\n",
    "import os\n",
    "from torchvision import transforms\n"
   ],
   "id": "1d4030ba85382e73",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "class MyData(Dataset):\n",
    "    def __init__(self, root_dir, label_dir, transform=None):\n",
    "        self.root_dir = root_dir\n",
    "        self.label_dir = label_dir\n",
    "        self.transform = transform\n",
    "        self.label_path = os.path.join(self.root_dir, self.label_dir)\n",
    "        self.image_path = os.listdir(self.label_path)\n",
    "\n",
    "    def __len__(self):\n",
    "        return len(self.image_path)\n",
    "\n",
    "    def __getitem__(self, idx):\n",
    "        img_path = os.path.join(self.label_path, self.image_path[idx])\n",
    "        image = Image.open(img_path)\n",
    "        label = self.label_dir\n",
    "        if self.transform:\n",
    "            image = self.transform(image)\n",
    "        return image, label"
   ],
   "id": "caa8e2c3881ecedf",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": "os.getcwd()",
   "id": "2ddd9cae2e542a44",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "root_dir = 'dataset/hymenoptera/train'\n",
    "label_dir = 'ants'\n",
    "transform = transforms.Compose([transforms.Resize(256), transforms.ToTensor()])\n",
    "original_data = MyData(root_dir, label_dir)\n",
    "original_img, label = original_data[0]\n",
    "type(original_img)"
   ],
   "id": "619ca51cdd26464a",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "transform_data = MyData(root_dir, label_dir, transform=transform)\n",
    "trans_img, label = transform_data[0]\n",
    "trans_img"
   ],
   "id": "6edee0b429601f6e",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": "trans_img.size()",
   "id": "7dc79dfb7a07a3d1",
   "outputs": [],
   "execution_count": null
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
